/*
 * @Description: 
 * @Author: wangqi
 * @Date: 2021-05-08 22:49:56
 * @LastEditTime: 2021-06-12 18:24:11
 */
import vnode from './vnode';

/*
    vnode(sel, data, children, text, elm)
    实现低配版本的 h函数, 需要传递三个参数，满足以下三种形态:

    1)  h('div', {}, '文字')
    2)  h('div', {},  [])
    3)  h('div', {}, h())
*/

function h(sel, data, c) {
    if (arguments.length != 3) {
        throw new Error('必须传入三个参数');
    }
    // ==================
    // 1)
    if (typeof c == 'string' || typeof c == 'number') {
        return vnode(sel, data, undefined, c, undefined)
        // ==================
        // 2)
    } else if (Array.isArray(c)) {
        // 如果是数组形式, 需要把该数组中的每一项收集起来
        let children = [];
        for (let i = 0; i < c.length; i++) {
            if (!(typeof c[i] == 'object' && c[i].hasOwnProperty('sel'))) {
                throw new Error('传入的数组参数有不是h函数')
            }
            children.push(c[i]);
        }
        return vnode(sel, data, children, undefined, undefined);

        // ==================
        // 3)
    } else if (typeof c == 'object' && c.hasOwnProperty('sel')) {
        let children = [c];
        return vnode(sel, data, children, undefined, undefined);

    } else {
        throw new Error('传入的第三个参数类型不对')
    }

};

export default h;
